### "Elitist Remedies: Complaint Resources and Representation in International Human Rights Bodies" ###
### Replication files for study 2 (individual level - Belarus) ###
### Author: Christoph Valentin Steinert ###

library(tidyverse)
library(readxl)
library(texreg)
library(dplyr)
library(Hmisc)
library(stringr)
library(texreg)
library(broom)
library(ggplot2)

# Load data
rm(list = ls())
bdata <- read_excel("analysisdata_study2_belarus.xlsx")

# Prepare variables
bdata <- bdata %>% rename(sentence_in_months = `sentence in months`)
bdata$sentence_in_months <- as.numeric(bdata$sentence_in_months)
bdata$population_size_hometown <- as.numeric(bdata$population_size_hometown)
bdata$comm_covered <- ifelse(is.na(bdata$comm_covered), 0, bdata$comm_covered)
bdata$unwgad_covered <- ifelse(is.na(bdata$unwgad_covered), 0, bdata$unwgad_covered)
bdata$Female <- ifelse(bdata$Gender == "Female", 1, 0)
bdata$spcovered <- bdata$comm_covered
bdata$spcovered <- ifelse(bdata$unwgad_covered == 1, 1, bdata$spcovered)
bdata$date_of_birth2 <- as.Date(bdata$date_of_birth, "%Y-%m-%d")
bdata$age <- as.Date("2023-04-01", "%Y-%m-%d") - bdata$date_of_birth2
bdata$age <- gsub(" days", "", bdata$age)
bdata$Age <- as.numeric(bdata$age)
bdata$Capital_resident <- ifelse(bdata$population_size_hometown == "1996553", 1, 0)
bdata$Capital_resident <- ifelse(is.na(bdata$Capital_resident), 0, bdata$Capital_resident)
bdata$Charged <- str_detect(bdata$Charges.indicted, "Criminal")
bdata$Charged <- ifelse(bdata$Charged == T, 1, 0)
bdata$Charge_incite <- str_detect(bdata$Charges.indicted, "Art. 130")
bdata$Charge_incite <- ifelse(bdata$Charge_incite == T, 1, 0)
bdata$Charge_riots <- str_detect(bdata$Charges.indicted, "Art. 293")
bdata$Charge_riots <- ifelse(bdata$Charge_riots == T, 1, 0)
bdata$ins1 <- str_detect(bdata$Charges.indicted, "Art. 368")
bdata$ins2 <- str_detect(bdata$Charges.indicted, "Art. 369")
bdata$Charge_insult <- bdata$ins1 + bdata$ins2
bdata$Charge_insult <- ifelse(is.na(bdata$Charge_insult), 0, bdata$Charge_insult)
bdata$Charge_insult <- ifelse(bdata$Charge_insult == 2, 1, bdata$Charge_insult)
bdata$City_resident <- ifelse(bdata$population_size_hometown > "100000", 1, 0)
bdata$City_resident <- ifelse(is.na(bdata$population_size_hometown), 0, bdata$City_resident)

# Prepare occupation dummies
unique(bdata$occupation_general)
bdata$law1 <- str_detect(bdata$occupation_general, "lawyer")
bdata$law2 <- str_detect(bdata$occupation_general, "Lawyer")
bdata$law3 <- str_detect(bdata$occupation_general, "Prosecuter")
bdata$law4 <- str_detect(bdata$occupation_general, "Former Legal Adviser")
bdata$Lawyer <- bdata$law1 + bdata$law2 + bdata$law3 + bdata$law4
bdata$Lawyer <- ifelse(is.na(bdata$Lawyer), 0, bdata$Lawyer)
table(bdata$Lawyer) # 14 lawyer
bdata$jour1 <- str_detect(bdata$occupation_general, "journalist")
bdata$jour2 <- str_detect(bdata$occupation_general, "Journalist")
bdata$jour3 <- str_detect(bdata$occupation_general, "blogger")
bdata$jour4 <- str_detect(bdata$occupation_general, "editor-in-chief")
bdata$jour5 <- str_detect(bdata$occupation_general, "vlogger")
bdata$jour6 <- str_detect(bdata$occupation_general, "Editor")
bdata$jour7 <- str_detect(bdata$occupation_general, "Blogger")
bdata$Journalist <- bdata$jour1 + bdata$jour2 + bdata$jour3 + bdata$jour4 + bdata$jour5 +
  bdata$jour6 + bdata$jour7
bdata$Journalist <- ifelse(is.na(bdata$Journalist), 0, bdata$Journalist)
table(bdata$Journalist) # 32 journalists
bdata$stud1 <- str_detect(bdata$occupation_general, "student")
bdata$stud2 <- str_detect(bdata$occupation_general, "Student")
bdata$Student <- bdata$stud1 + bdata$stud2
bdata$Student <- ifelse(is.na(bdata$Student), 0, bdata$Student)
table(bdata$Student) # 17 students
bdata$chair1 <- str_detect(bdata$occupation_general, "Chair person")
bdata$chair2 <- str_detect(bdata$occupation_general, "chairwoman")
bdata$chair3 <- str_detect(bdata$occupation_general, "chairperson")
bdata$chair4 <- str_detect(bdata$occupation_general, "executive director")
bdata$chair5 <- str_detect(bdata$occupation_general, "vice-president")
bdata$chair6 <- str_detect(bdata$occupation_general, "General director")
bdata$chair7 <- str_detect(bdata$occupation_general, "director")
bdata$chair8 <- str_detect(bdata$occupation_general, "Director")
bdata$chair9 <- str_detect(bdata$occupation_general, "head")
bdata$chair10 <- str_detect(bdata$occupation_general, "Chairman")
bdata$Chairperson <- bdata$chair1 + bdata$chair2 + bdata$chair3 +
  bdata$chair4 + bdata$chair5 + bdata$chair6 + bdata$chair7 + bdata$chair8 +
  bdata$chair9 + bdata$chair10
bdata$Chairperson <- ifelse(is.na(bdata$Chairperson), 0, bdata$Chairperson)
bdata$Chairperson <- ifelse(bdata$Chairperson == 2, 1, bdata$Chairperson)
table(bdata$Chairperson) # 13 chairpersons
bdata$emp1 <- str_detect(bdata$occupation_general, "employee")
bdata$emp2 <- str_detect(bdata$occupation_general, "Employee")
bdata$emp3 <- str_detect(bdata$occupation_general, "acountant")
bdata$emp4 <- str_detect(bdata$occupation_general, "acountant")
bdata$emp5 <- str_detect(bdata$occupation_general, "press secretary")
bdata$emp6 <- str_detect(bdata$occupation_general, "driver")
bdata$emp7 <- str_detect(bdata$occupation_general, "postman")
bdata$emp8 <- str_detect(bdata$occupation_general, "operator")
bdata$emp9 <- str_detect(bdata$occupation_general, "sales manager")
bdata$emp10 <- str_detect(bdata$occupation_general, "IT worker")
bdata$employee <- bdata$emp1 + bdata$emp2 + bdata$emp3 + bdata$emp4 +
  bdata$emp5 + bdata$emp6 + bdata$emp7 + bdata$emp8 + bdata$emp9 +
  bdata$emp10 
bdata$employee <- ifelse(is.na(bdata$employee), 0, bdata$employee)
bdata$employee <- ifelse(bdata$employee == 2 , 1, bdata$employee)
table(bdata$employee) # 28 employees (very heterogenous, better as reference category)
bdata$entre1 <- str_detect(bdata$occupation_general, "entrepreneur")
bdata$entre2 <- str_detect(bdata$occupation_general, "owner")
bdata$entre3 <- str_detect(bdata$occupation_general, "owner")
bdata$entre4 <- str_detect(bdata$occupation_general, "businessman")
bdata$Entrepreneur <- bdata$entre1 + bdata$entre2 + bdata$entre3 + bdata$entre4
bdata$Entrepreneur <- ifelse(is.na(bdata$Entrepreneur),0, bdata$Entrepreneur)
bdata$Entrepreneur <- ifelse(bdata$Entrepreneur == 2,1, bdata$Entrepreneur)
table(bdata$Entrepreneur) # 11 entrepreneurs
bdata$mil1 <- str_detect(bdata$occupation_general, "military")
bdata$mil2 <- str_detect(bdata$occupation_general, "Military")
bdata$mil3 <- str_detect(bdata$occupation_general, "Armed")
bdata$mil4 <- str_detect(bdata$occupation_general, "KGB")
bdata$mil5 <- str_detect(bdata$occupation_general, "colonel")
bdata$Military <- bdata$mil1 + bdata$mil2 + bdata$mil3 + bdata$mil4 +
  bdata$mil5
bdata$Military <- ifelse(is.na(bdata$Military), 0, bdata$Military)
bdata$Military <- ifelse(bdata$Military == 2, 1, bdata$Military)
table(bdata$Military) # 8 military
bdata$aca1 <- str_detect(bdata$occupation_general, "professor")
bdata$aca2 <- str_detect(bdata$occupation_general, "Researcher")
bdata$aca3 <- str_detect(bdata$occupation_general, "Lecturer")
bdata$Professor <- bdata$aca1 + bdata$aca2 + bdata$aca3 
bdata$Professor <- ifelse(is.na(bdata$Professor), 0, bdata$Professor)
table(bdata$Professor) # 6 professors
bdata$doc1 <- str_detect(bdata$occupation_general, "doctor")
bdata$doc2 <- str_detect(bdata$occupation_general, "Doctor")
bdata$Doctor <- bdata$doc1 + bdata$doc2
bdata$Doctor <- ifelse(is.na(bdata$Doctor), 0, bdata$Doctor)
table(bdata$Doctor) # 4 doctors
bdata$art1 <- str_detect(bdata$occupation_general, "artist")
bdata$art2 <- str_detect(bdata$occupation_general, "Artist")
bdata$art3 <- str_detect(bdata$occupation_general, "musician")
bdata$art4 <- str_detect(bdata$occupation_general, "Musician")
bdata$Artist <- bdata$art1 + bdata$art2 + bdata$art3 + bdata$art4
bdata$Artist <- ifelse(is.na(bdata$Artist), 0, bdata$Artist)
table(bdata$Artist) # 11 artists
bdata$jour1 <- bdata$jour2 <- bdata$jour3 <- bdata$law1 <- bdata$law2 <- bdata$stud1 <-
  bdata$stud2 <- bdata$law3 <- bdata$jour4 <- bdata$chair1 <- bdata$chair2 <-
  bdata$chair3 <- bdata$chair4 <- bdata$chair5 <- bdata$law4 <- bdata$chair6 <-
  bdata$chair7 <- bdata$chair8 <- bdata$chair9 <- bdata$jour5 <- bdata$jour6 <-
  bdata$emp1 <- bdata$emp2 <- bdata$emp3 <- bdata$emp4 <- bdata$emp5 <- bdata$emp6 <-
  bdata$emp7 <- bdata$emp8 <- bdata$emp9 <- bdata$emp10 <- 
  bdata$mil1 <- bdata$mil2 <- bdata$mil3 <- bdata$mil4 <- bdata$mil5 <-
  bdata$aca1 <- bdata$aca2 <- bdata$aca3 <- bdata$doc1 <- bdata$doc2 <-
  bdata$aca6 <- bdata$jour7 <- bdata$chair10 <- bdata$entre1 <-
  bdata$entre2 <- bdata$entre3 <- bdata$entre4 <- bdata$art1 <-
  bdata$art2 <- bdata$art3 <- bdata$art4 <- bdata$ins1 <- bdata$ins2 <- NULL

# Check distributions
hist(bdata$Age)
hist(bdata$sentence_in_months)
bdata$Sentence_log <- log(1 + bdata$sentence_in_months)
hist(bdata$Sentence_log)
bdata$Age_under25 < ifelse(bdata$Age < 9125, 1, 0)


# Modelling the selection process
m1 <- glm(spcovered ~ Female + Doctor + Professor +
            Lawyer + Journalist  + Chairperson + 
            Capital_resident, family = binomial(link = logit), data = bdata)
screenreg(m1)
m2 <- glm(spcovered ~ Female + Doctor + Professor +
            Lawyer + Journalist + Chairperson + 
            Capital_resident + Sentence_log +
            Charge_incite + Charge_riots + Charge_insult, 
          family = binomial(link = logit), data = bdata)
screenreg(m2)


# Create coefficient plot of main model
results_m2 <- m2 %>% tidy()
results_m2 <- results_m2 %>% mutate(lower = estimate - 1.96*std.error, 
                                      upper = estimate + 1.96*std.error, 
                                      lower_99 = estimate -	2.576*std.error, 
                                      upper_99 = estimate +	2.576*std.error)

results_m2$term[results_m2$term == "Charge_incite"] <- "Charge incite"
results_m2$term[results_m2$term == "Charge_riots"] <- "Charge riots"
results_m2$term[results_m2$term == "Charge_insult"] <- "Charge insult"
results_m2$term[results_m2$term == "Capital_resident"] <- "Capital resident"
results_m2$term[results_m2$term == "Sentence_log"] <- "Sentence log"

coefs <- factor(results_m2$term, levels = rev(c("Lawyer", "Professor",
                                                 "Doctor", "Journalist", 
                                                "Chairperson", 
                                                 "Capital resident",
                                                 "Female", "Sentence log",
                                                 "Charge incite", "Charge riots", "Charge insult",
                                                 "(Intercept)")))


#pdf(file = "Figure7.pdf")
#tiff("Figure7.tiff", units="in", width=5, height=5, res=1200)
ggplot(results_m2) +
  geom_linerange(aes(ymin = lower_99, ymax = upper_99,
                     y = estimate, x = coefs), 
                 position = position_dodge(width = 0.5), 
                 color = "gray40", size = 0.75) +
  geom_linerange(aes(ymin = lower, ymax = upper,
                     y = estimate, x = coefs), 
                 position = position_dodge(width = 0.5), 
                 color = "gray80", size = 1.25) +
  geom_point(aes(y = estimate, x = coefs), position = position_dodge(width = 0.5), size = 2) +
  geom_hline(yintercept = 0, lty = 2) +
  xlab("") +
  ylab("Log odds of being covered by the UNSP") +
  coord_flip() +
  theme_bw() +
  theme(plot.title = element_text(hjust = 0.5)) +
  ggtitle("")
#dev.off()
#dev.off()

# End
